﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.Menu;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.System.Interface;

namespace WMS.FC.Domain.Service.System
{
    public class GetPageConfigService : ServiceBase, IGetPageConfigService
    {

        protected readonly IMenuRepository menuRepository;

        public GetPageConfigService(ILogger<ServiceBase> logger, IOptions<WmsSettings> options, IMapper mapper, IMenuRepository menuRepository) : base(logger, options, mapper)
        {
            this.menuRepository = menuRepository;
        }

        public async Task<ActionResult<PageConfigDO>> GetPageConfig(string menuId, UserInfo userInfo)
        {
            var result = new ActionResult<PageConfigDO>("获取菜单配置");
            try
            {
                if (string.IsNullOrWhiteSpace(menuId))
                    return result.ArgumentError("菜单ID不能为空");

                var menuPO = await menuRepository.GetMenuWithSubMenu(Convert.ToInt64(menuId));
                if (menuPO == null)
                    return result.DbError($"ID:{menuId},菜单数据未找到");

                result.Data = new PageConfigDO()
                {
                    MenuConfig = mapper.Map<MenuConfigDO>(menuPO)
                };
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
